clear;
clc;
close all;
addpath('../02_Functions');


%------------------------------------------------------------------------%
% This file creates figure 5 in the paper 
%------------------------------------------------------------------------%


% load -----------------------------------------------------------------------

file_name = '../01_data/es_quotes_5minute_EST';

% Call returns routine -----------------------------------------------------------------------

legend_txt = {'98.01 - 10.12' ; '11.01 - 20.12'} ;

for c = 1 : 2
    
    if c == 1
        start_date     = datenum(1998,1,5);
        finish_date    = datenum(2010,12,31);
    elseif c == 2
        start_date     = datenum(2011,1,5);
        finish_date    = datenum(2020,12,31);
    end
    
    
    winsorizing    = 0;
    alpha          = 0.99;
    replace_option = 0;
    
    results         = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
    return_AD       = results.return_AD;
    return_ON       = results.return_ON;
    return_ID       = results.return_ID;
    log_return_AD   = results.log_return_AD;
    log_return_ON   = results.log_return_ON;
    log_return_ID   = results.log_return_ID;
    cum_log_returns = results.cum_log_returns;
    caldt_mat       = results.caldt_mat;
    T               = results.T;
    
    % LOG RETURNS -----------------------------------------------------------------------
    
    mu_log_return_AD = 100*252*nanmean(log_return_AD,1);
    mu_log_return_ON = 100*252*nanmean(log_return_ON,1);
    mu_log_return_ID = 100*252*nanmean(log_return_ID,1);
    
    % mean cum log returns and standard error across intervals
    mu_cum_log_returns = 100*252*[0 , nanmean(cum_log_returns,1)];
    var_cum_log_returns = [NaN ,nanvar(100*252*cum_log_returns,1)];
    se_cum_log_returns = sqrt(var_cum_log_returns/T);
    
    % annualized mean of HbH log return
    mu_log_return_AD_HbH = 100*252*nanmean(results.log_return_HbH_AD);
    
    % SIMPLE RETURNS -----------------------------------------------------------------------
    
    mu_return_AD = 100*((nangeomean(1+return_AD,1)).^252-1);
    mu_return_ON = 100*((nangeomean(1+return_ON,1)).^252-1);
    mu_return_ID = 100*((nangeomean(1+return_ID,1)).^252-1);
    
    % annualized mean of HbH simple return
    mu_return_AD_HbH = 100*((nangeomean(1+results.return_HbH_AD,1)).^252-1);
        
    % mean cumulative investment to 1 dollar
    mu_cum_returns_one_dollar_inv = [1 , nangeomean(results.cum_returns,1)].^(252);
    se_cum_returns_one_dollar_inv = se_cum_log_returns/100;
    
    % SPECIAL HOURS
    if c == 1
        special_hours = zeros(1,24);
        special_hours(1,9) = mu_log_return_AD_HbH(1,9);
        special_hours(1,16) = mu_log_return_AD_HbH(1,16);
    elseif c == 2
        special_hours = zeros(1,24);
        special_hours(1,9) = mu_log_return_AD_HbH(1,9);
        special_hours(1,24) = mu_log_return_AD_HbH(1,24);
    end
    
    % ---------------------------------------------------------------------------------- %
    
    % combine hourly plot and cumulative plot
    
    
    hour_index_1        = 1 : 289;
    x_ticks_1           = 1 : 12 : 289;
    hour_string_1       = {'18', '19', '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04' , '05' , '06' , '07' , '08' , '09' , '10' , '11' , '12' , '13', '14' , '15' , '16', '17' , '18'};
    
    hour_index_2        = 1 : 24;
    x_ticks_2           = 1 : 24;
    hour_string_2       = {'18', '19', '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04' , '05' , '06' , '07' , '08' , '09' , '10' , '11' , '12' , '13', '14' , '15' , '16', '17' , '18'};
    
    y_ticks             = -10 : 1 : 5;
    y_string            = {'-10','-9', '-8', '-7', '-6', '-5', '-4' , '-3' , '-2', '-1', '0', '1', '2', '3', '4', '5'};
    
    lightblue           = [91, 207, 244] / 255;
    lightgrey           = [0.7  0.7  0.7];
    
    LW = 5;
    
    figure
    p=bar(hour_index_2, mu_log_return_AD_HbH, 1, 'k');
    set(p,'FaceColor', lightgrey);
    hold on
    p2=bar(hour_index_2, special_hours, 1, 'k');
    set(p2,'FaceColor', lightblue); % add another color to special hours
    ylabel('avg. log returns (% p.a)')
    hold on
    xlim([hour_index_2(1)-0.5 hour_index_2(end)+0.5])
    ylim([-2 , 12])
    xtickangle(90)
    set(gca,'XTick',x_ticks_2,'XTickLabel',hour_string_2);
    hold on
    ax1 = gca;
    set(ax1,'XTick',[])
    set(gca, 'layer', 'bottom');
    grid on
    legend off
    hold on
    ax2_pos = ax1.Position;
    ax2 = axes('Position',ax2_pos,'XAxisLocation','bottom','YAxisLocation','right', 'Color','none');
    hold on
    l2 = line(hour_index_1,mu_cum_log_returns);
    l2.LineWidth = LW;
    l2.Color = 'black';
    xlim([1 289])
    ylim([-2 , 12])
    set(gca,'XTick',x_ticks_1,'XTickLabel',hour_string_1);
    hold on
    ylabel('Ann. avg. 5-min cum log return (in %, line)')
    xlabel('Trade Time (ET)')
    legend off
    
end



